import numpy as np
import pandas as pd

from sklearn.linear_model import LogisticRegression
from sklearn import datasets

iris = datasets.load_iris()

train_data = pd.read_csv('iris_train.data')
test_data = pd.read_csv('iris_test.data')

train_data['种类编码'] = train_data['种类'].map({'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2})
test_data['种类编码'] = test_data['种类'].map({'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2})

x_train = np.array(train_data.iloc[:, 0:4])
y_train = np.array(train_data.iloc[:, 5:6].values.ravel())
x_test = np.array(test_data.iloc[:, 0:4])
y_test = np.array(test_data.iloc[:, 5:6].values.ravel())

lr_model = LogisticRegression()
lr_model.fit(x_train, y_train)

bias = lr_model.intercept_
weight = lr_model.coef_

y_pred = lr_model.predict(x_test)

sum_mean = 0
for i in range(len(y_pred)):
    sum_mean += (y_pred[i] - y_test[i]) ** 2
sum_err = np.sqrt(sum_mean / len(y_pred))  # 测试级的数量

score = lr_model.score(x_test, y_test)

print('成功率: {:.3f}%'.format(score * 100))
